// 全局apiurl
// var urlpath = location.href.indexOf("?")+1;
// urlpath = location.href.substring(urlpath);
// var arr=urlpath.split("&");
// uesrMessage ={
// }
// arr.forEach(
//     function(item){
//         var brr = [];
//         brr=item.split("=");
//         debugger;
//         x[brr[0]]= brr[1];
//     }
// )
// var  userId = uesrMessage.userId;
// var  bzId = uesrMessage.bzId;
//值班表
var DateTime = new Date();
var year =  DateTime.getFullYear();
var month = DateTime.getMonth()+1;
var from = year+"-"+month+'-1';

var to = "";
// var baseUrl  = "http://10.36.1.67/bzapp/";
// var userId = ;
// var bzId = bzId;


var peopleList = [];
//获取班组简介
var fd = new FormData();
fd.append("json",JSON.stringify({
    "userId":userId,
    "data":{
        "bzId":bzId
    }
}));
// 人员简介字段渲染

$.ajax(
    {
        url:baseUrl+'api/Culture/GetMeetAbstract',
        data:{
            json:JSON.stringify({
                "userId":userId,
                "data":{
                    "bzId":bzId
                }
            })
        },
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            console.log(data);
            if(data.code==0){
                classinstru(data.data);
                $(".topLeft").html(data.data.BzName);
            }
            
        }
    }
)
function classinstru(item){
    var x ='<div class="m-item-j bg-color-font"><div class="circle bg-color-bg"></div><span>班组成员'+item.Member+'人</span><span>平均年龄'+(Number(item.AverageAage)).toFixed(0)+'岁</span></div>'+
   '<div class="m-item-j bg-color-font"><div class="circle bg-color-bg"></div><span>大专及以上学历'+item.Education+'人</span><span>中共党员'+item.PartyMember+'人</span></div>'+
   '<div class="m-item-j bg-color-font"><div class="circle bg-color-bg"></div><span>工程师'+item.Engineer+'人</span><span>助理工程师'+item.AssistantEngineer+'人</span><span>高级技师'+item.ExpertTechnician+'人</span><span>技师'+item.Technician+'人</span><span>高级工'+item.Expert+'人</span></div>'
   $("#m-job").html(x);
}
//获取文化墙信息
$.ajax(
    {
        // contentType:"application/json; charset=utf-8",
        url:baseUrl+'api/Culture/GetWallInfo',
        data:fd,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            if(data.code==0){
                mainclass(data.data);
                $(".topLeft").html(data.data.BzName);
            }
        }
    }
)
function mainclass(item){
    $(".textareaW").html(item.summary);
    // $("#classhope").val('是滴是滴')
    $("#classhope").val(item.vision);
     if(item.slogan){
                var xstring = item.slogan;
                if(xstring.length>20){
                   xstring =  xstring.substring(0,20)+"...";
                }
                $(".titlep").html(xstring);
        }
    // console.log(document.getElementById("#classhop"))
    // document.getElementById("#classhop").innerHTML="11111"
    var arr =[];//班组荣誉
    var brr = [];//风采剪影
    item.pics.forEach(
        function(obj){
            if(obj.filetype==1){
                arr.push(obj);
            }else{
                brr.push(obj);
            }
        }
    )
    console.log(arr,brr);
    var arrObj = "";
    var brrObj = "";
    if(arr.length>=4){
       arr =  arr.slice(0,4);
    }
    for(var j=0;j<arr.length;j++){
        arrObj +='<div class="honer-class">'+
           '<img src="'+arr[j].filepath+'" alt="">'+
        '</div>'
    }
    $(".honer-btn").html(arrObj);
    // for(var h=0;h<brr.length;h++){
    //     brrObj += '<div class="c-c-left">'+
    //           '<img src="'+brr[h].filepath+'" alt="">'+
    //     '</div>'
        
    // }
    if(brr.length==1){
        brrObj +='<div class="img-left">'+
                    '<img src="'+brr[0].filepath+'" alt="">'+
                '</div>'+
                '<div class="img-right">'+
            '</div>'
    }
    if(brr.length==2){
        brrObj +='<div class="img-left">'+
                '<img src="'+brr[0].filepath+'" alt="">'+
            '</div>'+
            '<div class="img-right">'+
            '<img src="'+brr[1].filepath+'" alt="">'+
        '</div>'
    }
    if(brr.length>=3){
        brrObj +='<div class="img-left">'+
                    '<img src="'+brr[0].filepath+'" alt="">'+
                '</div>'+
                '<div class="img-right">'+
                '<img src="'+brr[1].filepath+'" alt="">'+
                '<img  class="img-img" src="'+brr[2].filepath+'" alt="">'+
            '</div>'
    }
    $(".honner-imgs").html(brrObj);
}
//保存、编辑文化墙信息
var SaveWallInfo = new FormData();
var saveParams ={
       userId:userId,
       data:{
        bzId:bzId,
        summary:"aaaaa",
        concept:"dssssssssssssssssssssss",
        slogan:"bbbbb",
        vision:"cccc"
       } 
}
SaveWallInfo.append(
    'json',JSON.stringify(saveParams)
)
$.ajax(
    {
        url:baseUrl+'api/Culture/SaveWallInfo',
        data:SaveWallInfo,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            console.log(data);
        }
    }
)
//上传风采剪影、班组荣誉
var UpdateFile = new FormData();
var UpdateFileInfo ={
       userId:userId,
       data:{
        bzId:bzId,
        deletefileids:"",
        description:"",
        filetype:"0",
       } 
}
UpdateFile.append(
    'json',JSON.stringify(UpdateFileInfo)
)
$.ajax(
    {
        url:baseUrl+'api/Culture/UploadFile',
        data:UpdateFile,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            console.log(data);
        }
    }
)
//获取班组成员
var classpeople = new FormData();
$.ajax(
    {
        url:baseUrl+'api/Culture/GetDeptMembers',
        type:"POST",
        data:{
            json:JSON.stringify({
                "userId":userId,
                "data":{
                    "bzId":bzId
                }
            })
        },
        success:function(data){
            if(data.code==0){
                peopleList = data.data.slice(1);
                if(peopleList.length>14){
                    peopleList = peopleList.slice(0,14)
                }
                 var aa = '<img src="'+(data.data[0].Photo?data.data[0].Photo:'./img/04/3.png')+'" alt="">'+
                              '<div class="c-c-n-n bg-color-bg">'+
                                  '<div class="c-c-z">'+data.data[0].Name+'</div>'+
                                  '<div class="c-c-bz">'+data.data[0].Quarters+'</div>'+
                              '</div>'
                $(".c-name-left").html(aa)
                getperson();
            }
            console.log(peopleList);
            // peopleList = 
        }
    }
)
function getperson(){
    var x = "";
    for(var i=0;i<peopleList.length;i++){
        x += '<div class="person-list">'+
                    '<img src="'+(peopleList[i].Photo?peopleList[i].Photo:'./img/04/3.png')+'"" alt="">'+
                    '<div class="person-name">'+
                        '<div class="p-top bg-color-font">'+peopleList[i].Name+'</div>'+
                        '<div class="p-job bg-color-font">'+peopleList[i].Quarters+'</div>'+
                    '</div>'+
                '</div>'
    }
    $("#peopleQ").html(x);
}
//月度工作任务评分
$.ajax(
    {
        url:baseUrl+'api/Culture/GetMonthScore',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "year":year,
                    "month":month
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            // charts(data.data)
        }
    }
)
function charts(item) {
    var chartName = [];
    var chartValue = [];

    item.forEach(
        function(list){
            chartName.push(list.UserName);
            chartValue.push(list.Score);
        }
    )
    var obj = document.getElementById('charts')
    var data5 = [
        [10, 52, 25, 36, 30, 30, 29,9,9,9,9,9,9,9,9],
        [2, 3, 20, 34, 90, 0, 20,31,13,31,13,13,13,13],
        [0, 5, 20, 34, 30, 30, 22,45,45,45,45,45,45,5,45]
    ]
    var option = {
        tooltip: {
            trigger: 'axis'
        },
        xAxis: [{
            type: 'category',
            data: chartName,
            axisLine: {
                lineStyle: {
                    color: '#c85038'
                }
            }
        }],
        yAxis: [{
                type: 'value',
                min: 0,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        color: '#c85038'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#c85038'
                    }
                }
            },
            {
                type: 'value',
                min: 0,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        color: '#c85038'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#c85038'
                    }
                }

            }
        ],
        series: [
            {
                name: '任务评分',
                type: 'bar',
                barWidth: '20',
                data: chartValue,
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            position:'top',
                            color:'#c85038'
                        },
                        color: '#c85038',
                    },
                },
            },
        ]
    };
    var Chart4 = echarts.init(obj)
    Chart4.setOption(option);
}
//获取班组月考勤数据
$.ajax(
    {
        url:baseUrl+'api/Attendance/GetDeptAttendance',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "year":year,
                    "month":month
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            studyDate(data.data)
        }
    }
)

function studyDate(list){
    var arrlist = ""
    for(var i=0;i<list.length;i++){
        arrlist +='<div class="c-c-listmsg">'+
            '<div class="tv-title">'+
                '<div class="tv-img">'+
                    '<img src="'+list[i].Photo+'" alt="">'+
                '</div>'+
                '<div class="t-v-v">'+
                    '<div class="t-name">'+list[i].UserName+'</div>'+
                    '<div class="t-job-t">标准化管理员</div>'+
                '</div>'+
            '</div>'
            for(var j=0;j<list[i]['Data'].length;j++){
                if(list[i]['Data'][j].Category=="值班"||list[i]['Data'][j].Category=="出勤"){
                    arrlist += '<div class="to-works">'+
                    '<div class="page-number bg-color-font">'+list[i]['Data'][j].Times+'</div>'+
                    '<div class="page-num">值班(次)</div>'+
                    '</div>'
                }else{
                    arrlist += '<div class="to-works">'+
                    '<div class="page-number bg-color-font">'+list[i]['Data'][j].Hours+'</div>'+
                    '<div class="page-num">值班(h)</div>'+
                    '</div>'
                }
            }
            arrlist +='</div>'
    }
    $("#morework").html(arrlist);
}

if(month==12){
   to = (year+1)+'-1-1'
}else{
    to =year+"-"+(month+1)+'-1';
}
$.ajax(
    {
        url:baseUrl+'api/Attendance/GetDutyData',
        data:{json:JSON.stringify(
            {
                "userId":userId,
                "data":{
                    "from":year+"-"+month+'-1',
                    "to":to
                }
            }
        )},
        type:"POST",
        // contentType:false,
        // processData:false,
        success:function(data){
            var datapeople = data.data.pop();
            var xinqi = DateTime.getDay();
            var dateArr = [];
            data.data.forEach(
               function(item,index){
                 dateArr.push(
                     {
                        Data: item.Data,
                        index: index+1
                     }
                 )
               }
            )
            work(xinqi,dateArr)
        }
    }
)
//创建值班表
function work(index,list){
    var worklist = "";
    worklist += '<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr><tr>';
    var a = [];
    var c = [];
    var d = [];
    var DateTime = new Date();
    var day =  DateTime.getDate();
    day = day+index-1;
    


    for(var k=0;k<(index);k++){
        a.push(
            {
                Data: ""
            }
        )
    }
    c = a.concat(list);
    
    for(var w=0;w<(42-c.length)%7;w++){
        d.push(
            {
                Data: ""
            }
        )
    }
    c = c.concat(d);
    for(var q=0;q<c.length/7;q++){
        worklist += '<tr>'
        for(var o=0;o<7;o++){
            if((q*7+o)==day){
                worklist +='<td class="dayactive">'+
                '<span>'+((c[7*q+o].index)?(c[7*q+o].index):"")+'</span>'+
                '<p>'+((c[7*q+o].Data)?(c[7*q+o].Data):"")+'</p>'+
                '</td>'
            }else{
                worklist +='<td>'+
                '<span>'+((c[7*q+o].index)?(c[7*q+o].index):"")+'</span>'+
                '<p>'+((c[7*q+o].Data)?(c[7*q+o].Data):"")+'</p>'+
                '</td>'
            }
        }
        worklist += '</tr>'
    }
    $("#newtable").html(worklist);
}
//获取学习园地
$.ajax(
    {
        url:baseUrl+'api/Culture/GetLearningGardens',
        data:fd,
        type:"POST",
        contentType:false,
        processData:false,
        success:function(data){
            console.log(data);
            studyarea(data.data);
        }
    }
)
function studyarea(itemlist){
    var studyarealist = "";
    // studyarealist +='<h3><i>学</i><i>习</i><i>园</i><i>地</i></h3>';
    for(var i=0;i<itemlist.length;i++){
        studyarealist +='<a target="_blank" download href="'+itemlist[i].filepath+'">'+itemlist[i].filename+'</a>';
    }
    $("#h-commoms").html(studyarealist);
}